/**
 * 
 */
package se.ricardo.gamesorgs.client.modes.games;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import se.ricardo.gamesorgs.database.models.GameContainer;

/**
 * A {@link JPanel} containing a {@link JTabbedPane} with tables
 * for all different lists.
 * <br>
 * 
 * @author Ricardo Ocaña Leal
 *
 */
public class GamesPanel extends JPanel {
	protected JTabbedPane tabbedPanel;
	protected JTable[] gamesTables = new JTable[3];
	
	// Tabs in the panel
	public static final int COLLECTION = 0;
	public static final int WISH_LIST = 1;
	public static final int SALE_LIST = 2;

	/**
	 * Creates the JPanel with a {@link BorderLayout} as
	 * LayoutManager.
	 * */
	public GamesPanel() {
		super(new BorderLayout());
		setOpaque(true);
		setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 10));
		
		createTabbedPanel();
	}
	
	/**
	 * Creates several {@link JTable}s for displaying the lists of games.
	 * */
	private void createTabbedPanel() {
		tabbedPanel = new JTabbedPane();
		
		for(int i=0; i<gamesTables.length; i++) {
			// use a custom TableModel
			final JTable gamesTable = new JTable(new GamesTableModel());
			gamesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			gamesTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
			gamesTable.setFillsViewportHeight(true);
			gamesTable.setBorder(BorderFactory.createCompoundBorder());
			
			gamesTables[i] = gamesTable;
		}
		
		final JScrollPane scrollCollection =
				new JScrollPane(gamesTables[COLLECTION]);
		final JScrollPane scrollWish =
				new JScrollPane(gamesTables[WISH_LIST]);
		final JScrollPane scrollSale =
				new JScrollPane(gamesTables[SALE_LIST]);
		
		tabbedPanel.addTab("Collection", scrollCollection);
		tabbedPanel.addTab("Wish List", scrollWish);
		tabbedPanel.addTab("Sell List", scrollSale);
		
		add(tabbedPanel, BorderLayout.CENTER);
	}
	
	/**
	 * Returns the {@link TableModel} used by the {@link JTable}.
	 * <br>
	 * 
	 * @return
	 * An object to the {@link GamesTableModel} used.
	 * */
	protected TableModel getCurrentTableModel() {
		return gamesTables[getSelectedTabIndex()].getModel();
	}
	
	/**
	 * Returns the index of the currently selected tab.
	 * <br>
	 * 
	 * @return
	 * The index of the selected tab.
	 * */
	protected int getSelectedTabIndex() {
		return tabbedPanel.getSelectedIndex();
	}
	
	/**
	 * Returns the index of the currently selected game.
	 * <br>
	 * 
	 * @return
	 * The index of the game.
	 * */
	protected int getSelectedGameIndex() {
		return gamesTables[getSelectedTabIndex()].getSelectedRow();
	}
	
	
	/**
	 * Custom {@link TableModel} for displaying the games list
	 * in a {@link JTable}.
	 * */
	class GamesTableModel extends AbstractTableModel {
		// Default columns
		private String[] columns = {"Title", "Cartridge", "Instructions",
				"Box", "Comment"};
		private Object[][] data = {};
		private List<GameContainer> games;
		
		/**
		 * Called to update the {@link JTable} with new data.
		 * <br>
		 * 
		 * @param
		 * games A {@link List} containing {@link GameContainer} games.
		 * */
		public void setData(List<GameContainer> games) {
			this.games = games;
			data = new Object[games.size()][5];
			
			for(int i=0; i<games.size(); i++) {
				GameContainer g = games.get(i);
				
				Object[] temp = {
					g.getTitle(),
					g.getCartride(),
					g.getInstructions(),
					g.getBox(),
					g.getComment()
				};
				
				data[i] = temp;
			}
		}
		
		/**
		 * Called to clear the table of all the rows.
		 * */
		public void clearData() {
			data = new Object[0][5];
		}
		
		/**
		 * Returns a reference of the {@link GameContainer} at the
		 * given index.
		 * <br>
		 * 
		 * @param
		 * rowIndex The index of the game to return.
		 * <br>
		 * 
		 * @return
		 * Reference to a {@link GameContainer} object.
		 * */
		public GameContainer getGameAt(int rowIndex) {
			return games.get(rowIndex);
		}

		@Override
		public int getColumnCount() {
			return columns.length;
		}

		@Override
		public int getRowCount() {
			return data.length;
		}
		
		@Override
		public String getColumnName(int column) {
			return columns[column];
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}
	}
}
